<template>
  <div class="divBox voucher">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="filter-container">
          <el-form size="small" inline label-width="150px">
            <el-form-item
              :label="$t('timeselection')"
              class="width100 is_Scroll"
            >
              <el-radio-group
                v-model="tableFrom.date"
                type="button"
                class="mr20"
                size="small"
                @change="selectChange(tableFrom.date)"
              >
                <el-radio-button
                  v-for="(item, i) in fromList.fromTxt"
                  :key="i"
                  :label="item.val"
                  >{{ item.text }}</el-radio-button
                >
              </el-radio-group>
              <el-date-picker
                v-model="timeVal"
                value-format="yyyy/MM/dd"
                format="yyyy/MM/dd"
                size="small"
                type="daterange"
                placement="bottom-end"
                :placeholder="$t('customtime')"
                style="width: 250px"
                @change="onchangeTime"
              />
            </el-form-item>
            <el-form-item :label="$t('coupontype')" class="width100 ">
              <el-select
                v-model="tableFrom.coupon_type"
                :placeholder="$t('pleaseselect')"
                class="filter-item selWidth mr20"
                clearable
                @change="getList(1)"
              >
                <el-option :label="$t('all')" value="" />
                <el-option :label="$t('shopcoupon')" :value="0" />
                <el-option :label="$t('commoditycoupon')" :value="1" />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('couponname')" class="width100">
              <el-input
                v-model="tableFrom.coupon_name"
                @keyup.enter.native="getList(1)"
                :placeholder="$t('plecouponname')"
                class="selWidth mr20"
                @change="getList(1)"
                clearable
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  size="small"
                  class="el-button-solt"
                  @click="getList(1)"
                />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="small"
        highlight-current-row
      >
        <el-table-column prop="coupon_send_id" label="ID" min-width="80" />
        <el-table-column
          prop="title"
          :label="$t('nameofcoupon')"
          min-width="200"
        />
        <el-table-column :label="$t('typeofcoupon')" min-width="90">
          <template slot-scope="{ row }">
            <span>{{ row.type | couponTypeFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="create_time"
          min-width="200"
          :label="$t('datesent')"
        />
        <el-table-column min-width="200" :label="$t('validperiod')">
          <template slot-scope="{ row }">
            <div v-if="row.use_start_time && row.use_end_time">
              {{ row.use_start_time }} <br />- {{ row.use_end_time }}
            </div>
            <span v-else>{{ row.coupon_time }}{{ $t("day") }}</span>
          </template>
        </el-table-column>
        <el-table-column min-width="200" :label="$t('filtercriteria')">
          <template slot-scope="{ row }">
            <span v-if="!row.mark.search">{{ $t("nothing") }}</span>
            <div v-else>
              <span v-if="row.mark.search['用户标签']">
                用户标签:{{ row.mark.search["用户标签"] }}
              </span>
              <span v-if="row.mark.search['用户类型']">
                用户类型:{{ row.mark.search["用户类型"] }}
              </span>
              <span v-if="row.mark.search['性别']">
                性别:{{ row.mark.search["性别"] }}
              </span>
              <span v-if="row.mark.search['消费情况']">
                消费情况:{{ row.mark.search["消费情况"] }}
              </span>
              <span v-if="row.mark.search['身份']">
                身份:{{ row.mark.search["身份"] }}
              </span>
              <span v-if="row.mark.search['访问情况']">
                访问情况:{{ row.mark.search["访问情况"] }}
              </span>
              <span v-if="row.mark.search['访问时间']">
                访问时间:{{ row.mark.search["访问时间"] }}
              </span>
              <span v-if="row.mark.search['昵称']">
                昵称:{{ row.mark.search["昵称"] }}
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column min-width="200" :label="$t('usage')">
          <template slot-scope="{ row }">
            <div>
              <span>{{ $t("issuedquantity") }}：{{ row.coupon_num }}</span>
              <span class="sheng"
                >{{ $t("issuedandusedquantity") }}：{{ row.useCount }}</span
              >
            </div>
          </template>
        </el-table-column>

        <el-table-column :label="$t('operation')" min-width="150" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              class="mr10"
              @click="details(scope.row)"
              >{{ $t("details") }}</el-button
            >
            <el-button
              type="text"
              size="small"
              class="mr10"
              @click="usedRecord(scope.row.coupon_send_id)"
              >{{ $t("usagerecord") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!--优惠券详情-->
    <el-dialog
      :title="$t('coupondetails')"
      :visible.sync="detailDialog"
      width="700px"
      v-if="detailDialog"
    >
      <div>
        <div class="box-container">
          <div class="list sp">
            <label class="name">{{ $t("couponname") }}</label>
            <span class="info">{{ couponDetail.title }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("coupontype") }}：</label>
            <span class="info">{{ couponDetail.type | couponTypeFilter }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("couponfacevalue") }}：</label>
            <span class="info">{{ couponDetail.coupon_price }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("usethreshold") }}：</label>
            <span class="info">{{
              couponDetail.use_min_price == "0.00"
                ? $t("nothreshold")
                : $t("minimumconsumption") + couponDetail.use_min_price
            }}</span>
          </div>
          <div class="list sp100">
            <label class="name">{{ $t("validperiod") }}</label>
            <span
              v-if="couponDetail.coupon_time && couponDetail.coupon_type == 0"
              class="info"
              >{{ couponDetail.coupon_time }}{{ $t("day") }}</span
            >
            <span
              v-else-if="couponDetail.coupon_type == 1"
              class="info"
              style="font-size: 12px"
              >{{
                couponDetail.use_start_time + " - " + couponDetail.use_end_time
              }}</span
            >
          </div>
          <div class="list sp100">
            <label class="name">{{ $t("collectiontime") }}：</label>
            <span class="info" v-if="couponDetail.is_timeout == 1"
              >{{ couponDetail.start_time }} - {{ couponDetail.end_time }}</span
            >
            <span v-else class="info" style="font-size: 12px">{{
              $t("unlimitedtime")
            }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("type") }}：</label>
            <span class="info">{{
              couponDetail.send_type | couponUseTypeFilter
            }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("limitedornot") }}：</label>
            <span class="info">{{
              couponDetail.is_limited | filterClose
            }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("totalpublished") }}：</label>
            <span class="info">{{
              couponDetail.is_limited == 0
                ? $t("unlimited")
                : couponDetail.total_count
            }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("totalremaining") }}：</label>
            <span class="info">{{
              couponDetail.is_limited == 0
                ? $t("unlimited")
                : couponDetail.remain_count
            }}</span>
          </div>
          <!-- <div class="list sp">
            <label class="name">已领取总数：</label>
            <span class="info">{{ couponDetail.send_num  }}</span>           
          </div> -->
          <div class="list sp">
            <label class="name">{{ $t("totalsent") }}：</label>
            <span class="info">{{ couponDetail.coupon_num }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("totalused") }}：</label>
            <span class="info" style="color: red">{{
              couponDetail.useCount
            }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("filtercriteria") }}：</label>
            <span v-if="!couponDetail.mark.search">{{ $t("nothing") }}</span>
            <span v-else>
              <span
                class="condition"
                v-if="couponDetail.mark.search['用户标签']"
              >
                用户标签:{{ couponDetail.mark.search["用户标签"] }}
              </span>
              <span
                class="condition"
                v-if="couponDetail.mark.search['用户类型']"
              >
                用户类型:{{ couponDetail.mark.search["用户类型"] }}
              </span>
              <span class="condition" v-if="couponDetail.mark.search['性别']">
                性别:{{ couponDetail.mark.search["性别"] }}
              </span>
              <span
                class="condition"
                v-if="couponDetail.mark.search['消费情况']"
              >
                消费情况:{{ couponDetail.mark.search["消费情况"] }}
              </span>
              <span class="condition" v-if="couponDetail.mark.search['身份']">
                身份:{{ couponDetail.mark.search["身份"] }}
              </span>
              <span
                class="condition"
                v-if="couponDetail.mark.search['访问情况']"
              >
                访问情况:{{ couponDetail.mark.search["访问情况"] }}
              </span>
              <span
                class="condition"
                v-if="couponDetail.mark.search['访问时间']"
              >
                访问时间:{{ couponDetail.mark.search["访问时间"] }}
              </span>
              <span class="condition" v-if="couponDetail.mark.search['昵称']">
                昵称:{{ couponDetail.mark.search["昵称"] }}
              </span>
            </span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("sort") }}：</label>
            <span class="info">{{ couponDetail.sort }}</span>
          </div>
          <div class="list sp">
            <label class="name">{{ $t("state") }}：</label>
            <span class="info">{{
              couponDetail.status ? $t("open") : $t("close")
            }}</span>
          </div>
        </div>
      </div>
    </el-dialog>
    <!--领取记录-->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      min-width="500px"
      :before-close="handleClose"
      class="modalbox"
    >
      <div class="header clearfix">
        <div class="filter-container">
          <div class="demo-input-suffix acea-row">
            <span class="seachTiele">{{ $t("howtogetcoupons") }}</span>
            <el-select
              v-model="tableFromIssue.type"
              :placeholder="$t('pleaseselect')"
              class="filter-item selWidth mr20"
              clearable
              @change="getIssueList()"
            >
              <el-option :label="$t('all')" value="" />
              <el-option :label="$t('collectbyyourself')" value="receive" />
              <el-option :label="$t('backgroundsending')" value="send" />
              <el-option :label="$t('presentedbynewcomers')" value="new" />
              <el-option :label="$t('buysomethingforfree')" value="buy" />
            </el-select>
          </div>
        </div>
      </div>
      <el-table
        v-loading="Loading"
        :data="issueData.data"
        style="width: 100%; margin-top: 15px"
        size="small"
        highlight-current-row
      >
        <el-table-column
          prop="user.nickname"
          :label="$t('username')"
          min-width="120"
        />
        <el-table-column :label="$t('useravatar')" min-width="80">
          <template slot-scope="scope">
            <div
              v-if="scope.row.user && scope.row.user.avatar"
              class="demo-image__preview"
            >
              <img
                style="width: 36px; height: 36px"
                :src="scope.row.user.avatar"
              />
            </div>
            <div v-else class="demo-image__preview">
              <img
                style="width: 36px; height: 36px"
                src="../../../../assets/images/f.png"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column min-width="200" :label="$t('howtogetcoupons')">
          <template slot-scope="scope">
            <span>{{
              scope.row.type == "receive"
                ? $t("collectbyyourself")
                : scope.row.type == "send"
                ? $t("backgroundsending")
                : scope.row.type == "new"
                ? $t("newpeople")
                : "买赠送"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('usage')" min-width="180">
          <template slot-scope="scope">
            <span v-if="scope.row.use_time">{{ scope.row.use_time }}</span>
            <span v-else>{{ $t("notused") }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[10, 20, 30, 40]"
          :page-size="tableFromIssue.limit"
          :current-page="tableFromIssue.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="issueData.total"
          @size-change="handleSizeChangeIssue"
          @current-change="pageChangeIssue"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import { couponSendLstApi, issueApi, couponDetailApi } from "@/api/marketing";
import { roterPre } from "@/settings";
export default {
  name: "CouponList",
  data() {
    return {
      Loading: false,
      dialogVisible: false,
      detailDialog: false,
      roterPre: roterPre,
      listLoading: true,
      title: this.$t("receivingrecords"),
      receiveTime: this.$t("collectiontime"),
      receiveType: 0,
      timeVal: [],
      fromList: {
        title: this.$t("selectionperiod"),
        custom: true,
        fromTxt: [
          {
            text: this.$t("all"),
            val: ""
          },
          {
            text: this.$t("today"),
            val: "today"
          },
          {
            text: this.$t("yesterday"),
            val: "yesterday"
          },
          {
            text: this.$t("last7days"),
            val: "lately7"
          },
          {
            text: this.$t("last30days"),
            val: "lately30"
          },
          {
            text: this.$t("thismonth"),
            val: "month"
          },
          {
            text: this.$t("thisyear"),
            val: "year"
          }
        ]
      },
      tableData: {
        data: [],
        total: 0
      },
      tableFrom: {
        page: 1,
        limit: 20,
        status: "",
        coupon_name: "",
        coupon_type: "",
        send_type: "",
        date: ""
      },
      tableFromIssue: {
        page: 1,
        limit: 10,
        send_id: 0,
        type: "send"
      },
      issueData: {
        data: [],
        total: 0
      },
      couponDetail: {}
    };
  },
  mounted() {
    this.getList(1);
  },
  methods: {
    handleClose() {
      this.dialogVisible = false;
    },
    // 详情
    details(row) {
      console.log(row);
      this.detailDialog = true;
      this.couponDetail = row;
    },
    // 使用记录
    usedRecord(id) {
      this.dialogVisible = true;
      this.title = this.$t("usagerecord");
      this.receiveTime = this.$t("usagetime");
      this.receiveType = 1;
      this.tableFromIssue.send_id = id;
      this.tableFromIssue.type = "send";
      this.tableFromIssue.page = 1;
      this.getIssueList();
    },
    // 选择时间
    selectChange(tab) {
      this.timeVal = [];
      this.tableFrom.date = tab;
      this.getList(1);
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      this.tableFrom.date = e ? this.timeVal.join("-") : "";
      this.getList(1);
    },
    // 列表
    getIssueList() {
      this.Loading = true;
      issueApi(this.tableFromIssue)
        .then(res => {
          this.issueData.data = res.data.list;
          this.issueData.total = res.data.count;
          this.Loading = false;
        })
        .catch(res => {
          this.Loading = false;
          this.$message.error(res.message);
        });
    },
    pageChangeIssue(page) {
      this.tableFromIssue.page = page;
      this.getIssueList();
    },
    handleSizeChangeIssue(val) {
      this.tableFromIssue.limit = val;
      this.getIssueList();
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      couponSendLstApi(this.tableFrom)
        .then(res => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.listLoading = false;
        })
        .catch(res => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList("");
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList("");
    }
  }
};
</script>

<style scoped lang="scss">
.modalbox {
  ::v-deep.el-dialog {
    min-width: 550px;
  }
}
.selWidth {
  width: 260px !important;
}
.seachTiele {
  line-height: 35px;
}
.fa {
  color: #0a6aa1;
  display: block;
}
.sheng {
  color: #ff0000;
  display: block;
}
.ml20 {
  margin-left: 20px;
}
.box-container {
  overflow: hidden;
}
.box-container .list {
  float: left;
  line-height: 40px;
}
.box-container .sp {
  width: 50%;
}
.box-container .sp3 {
  width: 33.3333%;
}
.box-container .sp100 {
  width: 100%;
}
.box-container .list .name {
  display: inline-block;
  width: 120px;
  text-align: right;
  color: #606266;
}
.box-container .list .blue {
  color: #1890ff;
}
.box-container .list.image {
  margin-bottom: 40px;
}
.box-container .list.image img {
  position: relative;
  top: 40px;
}
.condition {
  display: block;
  margin-left: 120px;
  &:first-child {
    display: inline;
    margin-left: 0;
  }
}
</style>
